package com.msb.mall.seckill.schedule;

import com.msb.mall.seckill.service.SeckillSkuScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;


/**
 * 定时上架秒杀商品信息
 */
@Slf4j
@Component
public class SeckillSkuSchedule {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private SeckillSkuScheduleService scheduleService;

    /**
     * cron 定时任务执行时间
     */
    @Async
//    @Scheduled(cron = "* * 2 * * ? ")
    @Scheduled(cron = "0/5 * * * * ? ")
    public void seckill(){
        RLock lock = redissonClient.getLock("seckill:upload:lock");
        lock.lock(10, TimeUnit.SECONDS);
        try {
            log.info("定时秒杀商品上架开始......");
            scheduleService.uploadSeckillSku3Day();
        }catch (Exception e){
            lock.unlock();
        }
    }

}
